package org.example.springmvcdemo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        UserInfo userInfo = new UserInfo();
        userInfo.setName("zhangsan");
        userInfo.setId(12);
        userInfo.setAge(18);

        // 对象转 JSON
        String s = objectMapper.writeValueAsString(userInfo);

        System.out.println(s);

        // JSON 转 java对象
        UserInfo user1 = objectMapper.readValue(s, UserInfo.class);
        System.out.println(user1);
    }
}
